﻿using System;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using PlanningGIS.Win.Controls;

namespace PlanningGIS.TocContextMenu.Form
{
    /// <summary>
    /// 作用：选择字段
    /// 作者：
    /// 修改时间：20090619
    /// </summary>
    public partial class frmLabelPntChoiseField : Office2007Form
    {
        private IBasicOverposterLayerProperties4 m_pBasicOverposterLayerProperties;
        private IGeoFeatureLayer m_pGeoFeatLyr;

        public frmLabelPntChoiseField()
        {
            InitializeComponent();
        }

        public IGeoFeatureLayer GeoFeatLyr
        {
            set { m_pGeoFeatLyr = value; }
        }

        public IBasicOverposterLayerProperties4 BasicOverposterLayerProperties
        {
            set { m_pBasicOverposterLayerProperties = value; }
            get { return m_pBasicOverposterLayerProperties; }
        }

        #region FormLoad 
        private void frmLabelPntChoiseField_Load(object sender, EventArgs e)
        {
            IField pField;
            IFields pFields;
            bool bHasFindOneNumbricField = false;
            string sValue = ""; //如果该图层没有设置过控置角度的字段的话，则记录第一个数字型字段的名称赋给combox
            int iFieldIndex;

            pFields = m_pGeoFeatLyr.FeatureClass.Fields;

            //向下拉框中添加字段名称及其别名
            cmbFieldsName.Items.Clear();

            for (int i = 0; i < pFields.FieldCount; i++)
            {
                pField = pFields.get_Field(i);
                if (pField.VarType > 1 && pField.VarType < 6)
                {
                    cmbFieldsName.Items.Add(pField.Name + "【" + pField.AliasName + "】");
                    if (bHasFindOneNumbricField == false)
                    {
                        sValue = pField.Name + "【" + pField.AliasName + "】";
                        bHasFindOneNumbricField = true;
                    }
                }
            }

            //初始化字段下拉框
            if (m_pBasicOverposterLayerProperties.FeatureType == esriBasicOverposterFeatureType.esriOverposterPoint)
            {
                //如果找到控制字段的话
                if (m_pBasicOverposterLayerProperties.RotationField != "")
                {
                    iFieldIndex = pFields.FindField(m_pBasicOverposterLayerProperties.RotationField);
                    if (iFieldIndex > 0)
                    {
                        pField = pFields.get_Field(iFieldIndex);
                        cmbFieldsName.Text = pField.Name + "【" + pField.AliasName + "】";
                    }
                }
                    //否则，则使用一个数字型的字段进行初始化
                else
                    cmbFieldsName.Text = sValue;
            }

            //初始化旋转坐标系
            if (m_pBasicOverposterLayerProperties.RotationType == esriLabelRotationType.esriRotateLabelArithmetic)
                optArithmetic.Checked = true;
            else
                optGeographic.Checked = true;

            //初始化旋转角度是否在原有字段的基础上加90度
            chkRation90.Checked = m_pBasicOverposterLayerProperties.PerpendicularToAngle;
        }
        #endregion

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            string[] sFieldName;

            //设置控制旋转角度的字段
            if (cmbFieldsName.Text.Trim() == "")
            {
                MsgHelper.ShowInfomationMsgbox("请选择字段名！");
                return;
            }
            else
            {
                sFieldName = cmbFieldsName.Text.Split('【');
                m_pBasicOverposterLayerProperties.RotationField = sFieldName[0];
            }

            //设置旋转时的参照坐标系
            if (optGeographic.Checked)
                m_pBasicOverposterLayerProperties.RotationType = esriLabelRotationType.esriRotateLabelGeographic;
            else
                m_pBasicOverposterLayerProperties.RotationType = esriLabelRotationType.esriRotateLabelArithmetic;

            //设置是否加90度
            m_pBasicOverposterLayerProperties.PerpendicularToAngle = chkRation90.Checked;

            DialogResult = DialogResult.OK;
        }



    }
}